package com.example.demo.demos.controller;

import com.example.demo.common.domain.Result;
import com.example.demo.demos.domain.User;
import com.example.demo.demos.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate; // 直接使用配置类中的Bean
    @GetMapping("/{id}")
    public User getById( @PathVariable Long id){
        return userService.selectById(id);
    }

    @GetMapping
    public List<User> listUsers(@RequestParam(required = false) Integer age) {
        if (age != null) {
            return userService.selectByAge(age);
        }
        return userService.selectByName("小王");
    }

    @PostMapping
    public int createUser(@RequestBody User user) {
        return userService.insert(user);
    }

    @PutMapping("/{id}")
    public int updateUser(@PathVariable Long id, @RequestBody User user) {
        user.setId(id);
        return userService.updateById(user);
    }

    @DeleteMapping("/{id}")
    public int deleteUser(@PathVariable Long id) {
        return userService.removeById(id);
    }

    @GetMapping("/1/{id}")
    public Result<User> getById1(@PathVariable Long id){
        return Result.success(userService.selectById(id));
    }



}


